<template>
  <div v-if="visible" class="task-detail-modal" @click.self="closeModal">
    <div class="task-detail-content">
      <div class="task-detail-header">
        <h3><i class="el-icon-tickets"></i> 我的待办</h3>
        <el-button 
          icon="el-icon-close" 
          circle 
          size="small"
          class="header-close-btn"
          @click="closeModal"
        ></el-button>
      </div>
      <div class="task-detail-body">
        <div class="task-items">
          <div 
            v-for="(task, index) in tasks" 
            :key="index" 
            class="task-item" 
            :class="`priority-${task.priority}`"
          >
            <el-checkbox v-model="task.completed" />
            <div class="task-content">
              <div class="task-title">
                <span :class="{ completed: task.completed }">{{ task.title }}</span>
                <el-tag v-if="task.tag" size="small" :type="tagTypes[task.tag.type]">
                  {{ task.tag.text }}
                </el-tag>
              </div>
              <div class="task-meta">
                <span class="deadline">
                  <i class="el-icon-time"></i>
                  {{ task.deadline }}
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="task-detail-footer">
          <el-button type="primary" size="small" @click="handleAdd">+ 新建任务</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TaskDetail',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    tasks: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      tagTypes: {
        urgent: 'danger',
        important: 'warning',
        normal: 'info'
      }
    }
  },
  methods: {
    closeModal() {
      this.$emit('close')
    },
    handleAdd() {
      this.$emit('add-task')
      this.closeModal()
    }
  }
}
</script>

<style scoped>
.task-detail-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
}

.task-detail-content {
  background-color: #fff;
  border-radius: 4px;
  width: 80%;
  max-width: 600px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.task-detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  border-bottom: 1px solid #e6e6e6;
  background-color: #f8f8f8;
  border-radius: 4px 4px 0 0;
}

.task-detail-header h3 {
  margin: 0;
  font-size: 16px;
  color: #333;
  display: flex;
  align-items: center;
  font-weight: normal;
}

.task-detail-header h3 i {
  margin-right: 8px;
  color: #666;
}


.task-detail-body {
  padding: 20px;
  max-height: 60vh;
  overflow-y: auto;
}

.task-items {
  padding: 8px 0;
}

.task-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  transition: background 0.3s;
  border-left: 3px solid transparent;
}

.task-item:hover {
  background: #f5f7fa;
}

.task-item.priority-1 {
  border-left-color: #f5222d;
}

.task-item.priority-2 {
  border-left-color: #fa8c16;
}

.task-item.priority-3 {
  border-left-color: #1890ff;
}

.task-content {
  flex: 1;
  margin-left: 12px;
}

.task-title {
  display: flex;
  align-items: center;
  margin-bottom: 6px;
}

.task-title span {
  margin-right: 8px;
}

.task-title .completed {
  text-decoration: line-through;
  color: #999;
}

.task-meta {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #666;
}

.deadline {
  display: flex;
  align-items: center;
}

.deadline i {
  margin-right: 4px;
  font-size: 12px;
}

.task-detail-footer {
  padding: 16px 0 0;
  text-align: center;
  border-top: 1px solid #e8e8e8;
  margin-top: 16px;
}
</style>